require 'simple-json'
class Corpus < ActiveRecord::Base
  has_and_belongs_to_many :segments
  @@parser = JsonParser.new
  
  def to_setence_by_strings
    json_data = @@parser.parse(self.json)
    data = {"id" => self.id,"text" => self.text}
    
    #cashe left_side context
    index = -1
    if self.left_side.blank?
      data["left_side"] = Array.new
    else
      match_data = self.sequence.match(self.center)
      match_data.pre_match.scan(Regexp.new("\s")){index += 1}
      data["left_side"] = json_data[0..index]
      if data["left_side"].size > 10
        data["left_side"] = data["left_side"][-10..-1]
      end
    end
    
    #cashe center context
    center_end = index + self.center.split(Regexp.new("\s")).size
    index += 1
    data["center"] = json_data[index..center_end]
    
    #cashe right_side context
    if self.right_side.blank?
      data["right_side"] = Array.new
    else
      center_end += 1
      data["right_side"] = json_data[center_end..-1]
      if data["right_side"].size > 10
        data["right_side"] = data["right_side"][0..9]
      end
    end
    
    return data
  end
  
  def to_sentence_by_words
    json_data = @@parser.parse(self.json)
    data = {"id" => self.id,"text" => self.text}
    
    #cashe left_side context
    index = -1
    if self.left_side.blank?
      data["left_side"] = Array.new
    else
      escaped = Regexp.escape(self.center)
      match_data = self.sequence.match(Regexp.new("\s#{escaped}\s|\s#{escaped}$"))
      index += 1
      match_data.pre_match.scan(Regexp.new("\s")){index += 1}
      data["left_side"] = json_data[0..index]
      if data["left_side"].size > 10
        data["left_side"] = data["left_side"][-10..-1]
      end
    end
    
    #cashe center context
    index += 1
    data["center"] = [json_data[index]]
    
    #cashe right_side context
    if self.right_side.blank?
      data["right_side"] = Array.new
    else
      index += 1
      data["right_side"] = json_data[index..-1]
      if data["right_side"].size > 10
        data["right_side"] = data["right_side"][0..9]
      end
    end
    
    return data
  end
  
  def to_sentence_by_word_sequence
    json_data = @@parser.parse(self.json)
    data = {"id" => self.id,"text" => self.text}
    
    #cashe left_side context
    index = -1
    if self.left_side.blank?
      data["left_side"] = Array.new
    else
      match_data = self.data.match("#{self.left_side}#{self.center}#{self.right_side}")
      match_data.pre_match.scan(Regexp.new("\t")){index += 1}
      index += 1
      data["left_side"] = json_data[0..index]
      if data["left_side"].size > 10
        data["left_side"] = data["left_side"][-10..-1]
      end
    end
    
    #cashe center context
    index += 1
    data["center"] = [json_data[index]]
    
    #cashe right_side context
    if self.right_side.blank?
      data["right_side"] = Array.new
    else
      index += 1
      data["right_side"] = json_data[index..-1]
      if data["right_side"].size > 10
        data["right_side"] = data["right_side"][0..9]
      end
    end
    
    return data    
  end

end
